package lib
import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"app/conf"
)

type Product struct {
	gorm.Model
	Code string
	Price uint
}
var odb *gorm.DB
func InitOrmDb() {
	var err error
	odb, err = gorm.Open("mysql", conf.MYSQL_USERNAME+":"+conf.MYSQL_PASSWORD+"@tcp("+conf.MYSQL_HOSTNAME+":"+conf.MYSQL_HOSTPORT+")/"+conf.MYSQL_BASE+"?charset=utf8mb4&parseTime=True&loc=Local")

	//更改表前缀
	gorm.DefaultTableNameHandler = func (db *gorm.DB, defaultTableName string) string  {
		return "app_" + defaultTableName;
	}

	if err != nil {
		panic("连接数据库失败")
	}else{
		println("ok")
	}
}

func GetOdb()(*gorm.DB)  {
	return odb
}